package com.yanqu.road.server.cmd.activity.summerquiz;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summerquiz.SummerQuizConfig;
import com.yanqu.road.entity.activity.summerquiz.config.SummerQuizInfo;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerQuizProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.activity.summerquiz.SummerQuizActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_SUMMER_QUIZ_USER_GUESS, desc = "竞猜")
@CmdApi(reqMsg = SummerQuizProto.SummerQuizGuessActionReqMsg.class, respMsg = SummerQuizProto.SummerQuizGuessActionRespMsg.class)
public class SummerQuizGuessActionCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        SummerQuizConfig config = SummerQuizActivityMgr.getConfig();
        // 没有活动
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        SummerQuizProto.SummerQuizGuessActionReqMsg reqMsg = SummerQuizProto.SummerQuizGuessActionReqMsg.parseFrom(packet.getBytes());

        SummerQuizInfo summerQuizInfo = config.getSummerQuizInfo(reqMsg.getQuizId());
        if(summerQuizInfo == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

//        int num = reqMsg.getNum();
//        if(num <= 0){
//            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
//            return;
//        }
        int type = reqMsg.getType();
        if(type != 1 && type != 2){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        if(!SystemOpenMgr.systemOpen(player, eSystemId.SummerQuizActivity.getValue())){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        //竞猜时间限制
        long now = System.currentTimeMillis();
        if(now > summerQuizInfo.getEndGuizTime() || now < summerQuizInfo.getBeginGuizTime()){
            ret = GameErrorCode.E_SUMMER_QUIZ_NOT_IN_TIME;
            return;
        }

        //看竞猜币
        SummerGamesConfig gamesConfig = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (gamesConfig == null || !ActivityMgr.activityInShowTime(gamesConfig.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        //免费竞猜
//        Property cost = new Property();
//        cost.addProperty(guessGoodsId, BigInteger.valueOf(num));
//
//        if(!player.getModule(CurrencyModule.class).currencyIsEnough(cost)){
//            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
//            return;
//        }
//
//        player.getModule(PreReduceModule.class).preReduce(cost);


        SummerQuizProto.CrossSummerQuizGuessActionReqMsg.Builder crossMsg = SummerQuizProto.CrossSummerQuizGuessActionReqMsg.newBuilder();
        crossMsg.setActivityId(config.getActivityInfo().getActivityId());
        crossMsg.setGamesActivityId(gamesConfig.getActivityId());
        crossMsg.setNum(0);
        crossMsg.setType(type);
        crossMsg.setQuizId(reqMsg.getQuizId());
        crossMsg.setConsume("");
        player.sendPacket(Protocol.C_SUMMER_QUIZ_USER_GUESS, crossMsg);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SUMMER_QUIZ_USER_GUESS, SummerQuizProto.SummerQuizGuessActionRespMsg.newBuilder().setRet(ret));
        }
    }
}
